package com.sn.author.service.impl;

import com.github.pagehelper.PageInfo;
import com.sn.author.common.enums.CollectionStatusEnum;
import com.sn.author.common.enums.TaskTypeEnum;
import com.sn.author.dao.CollectionDataMapper;
import com.sn.author.dao.CollectionRecordMapper;
import com.sn.author.dao.TaskMapper;
import com.sn.author.domain.BaseResult;
import com.sn.author.domain.dto.CollectionDTO;
import com.sn.author.domain.dto.TaskBasisInfo;
import com.sn.author.domain.entity.CollectionData;
import com.sn.author.domain.entity.CollectionRecord;
import com.sn.author.domain.vo.NumberVo;
import com.sn.author.domain.vo.QueryTimeVo;
import com.sn.author.domain.vo.TaskVo;
import com.sn.author.service.dao.AccountService;
import com.sn.author.service.dao.CollectionService;
import com.sn.author.service.dao.TaskService;
import com.sn.author.utils.BeanUtils;
import com.sn.author.utils.DateUtils;
import com.sn.author.utils.Export;
import com.sn.author.utils.export.CollectionExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author xiaom
 * @Date 2020/10/26 10:25
 * @Version 1.0.0
 * @Description <>
 **/
@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private TaskService taskService;

    @Resource
    private CollectionRecordMapper collectionRecordMapper;

    @Resource
    private CollectionDataMapper collectionDataMapper;

    @Autowired
    private AccountService accountService;

    @Override
    public Boolean collection(NumberVo numberVo) {
        TaskVo taskVo = new TaskVo();
        taskVo.setCollectionNum(numberVo.getNum());
        taskVo.setType(TaskTypeEnum.COLLECTION.getType());
        taskVo.setVideoUrl(Collections.singletonList("1"));
        taskVo.setAccounts(Collections.singletonList(accountService.getOnlineAccount().getId() + ""));
        TaskBasisInfo taskBasisInfo = taskService.start(taskVo);
        insertCollectionRecord(numberVo.getNum(),taskBasisInfo.getTaskId());
        return true;
    }

    private void insertCollectionRecord(Integer num,Long taskId){
        CollectionRecord record = new CollectionRecord();
        record.setNumber(num);
        record.setStartTime(DateUtils.formatYMDHMS(new Date()));
        record.setTaskId(taskId);
        record.setStatus(CollectionStatusEnum.AWAIT.getStatus());
        collectionRecordMapper.insert(record);
    }

    @Override
    public BaseResult<List<CollectionDTO>> pageList(QueryTimeVo queryTimeVo) {
        startPage(queryTimeVo);
        List<CollectionRecord> list = collectionRecordMapper.selectFilter(queryTimeVo);
        PageInfo<CollectionRecord> pageInfo = new PageInfo<>(list);
        return BaseResult.success(BeanUtils.coventObject(pageInfo.getList(),CollectionDTO.class),pageInfo.getTotal());
    }

    @Override
    public void export(Long taskId) {
        Assert.notNull(taskId,"缺少taskId字段");
        List<CollectionData> list = collectionDataMapper.selectByTaskId(taskId);
        Assert.isTrue(!list.isEmpty(),"无数据可导出");
        String[] title = {"序号","昵称","用户ID","抖音号","头像"};
        Export export = new CollectionExport(Arrays.asList(title),"数据采集_" + DateUtils.formatDate(new Date(),DateUtils.DATE_FORMAT_YYYYMMDDHHMMSS));
        export.generateWorkBookTitle().writeData(list).download();
    }

    @Override
    public void saveCommitData(List<CollectionData> data) {
        collectionDataMapper.batchInsert(data);
    }

    @Override
    public void updateTaskStatus(String taskId, CollectionStatusEnum statusEnum) {
        collectionRecordMapper.updateStatusByTaskId(taskId,statusEnum.getStatus());
    }
}
